//
//  CJExten+Regex.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-14.
//

import Foundation

public extension String {
    
    /// 去除前后空格
    var cj_trimSpace: String {
        let value = self.trimmingCharacters(in: .whitespaces)
        return value
    }
    
    /// 去除前后换行
    var cj_trimLine: String {
        let value = self.trimmingCharacters(in: .newlines)
        return value
    }
    
    /// 去除前后空格和换行
    var cj_trimSpaceAndLines: String {
        let value = self.trimmingCharacters(in: .whitespacesAndNewlines)
        return value
    }
    
    /// 去除所有空格
    var cj_trimAllSpace: String {
        let value = self.replacingOccurrences(of: " ", with: "")
        return value
    }
    
    /// 去除所有换行
    var cj_trimAllLines: String {
        let  value = self.replacingOccurrences(of: "\n", with: "")
        return value
    }
    
    /// 去除所有空格和换行
    var cj_trimAllSpaceAndLines: String {
        var value = self.cj_trimAllSpace
        value = self.cj_trimAllLines
        return value
    }
    
    /// 使用正则表达式替换
    func cj_replace(regex: String, with: String, options: NSRegularExpression.Options = []) -> String {
        let regex = try! NSRegularExpression(pattern: regex, options: options)
        let value = regex.stringByReplacingMatches(in: self, options: [], range: NSMakeRange(0, self.count), withTemplate: with)
        return value
    }
}

public extension String {
    
    /// 判断包含中文符号
    var cj_includeChineseSymbol: Bool {
        let chinesePunctuation = ["。", "？", "！", "，", "、", "；", "：", "「", "」", "『", "』", "‘", "’", "“",
                                  "”", "（", "）", "〔", "〕", "【", "】", "—", "…", "–", "．", "《", "》", "〈", "〉",]
        for value in chinesePunctuation {
            if self.contains(value) {
                return true
            }
        }
        return false
    }
    
    /// 验证包含中文
    var cj_includeChinese: Bool {
        for value in self {
            if "\u{4E00}" <= value && value <= "\u{9FA5}" {
                return true
            }
        }
        return false
    }
    
}

public extension String {
    /// 验证数字
    var cj_isDecimal: Bool {
        if self.count <= 0 {
            return false
        }
        let value = self.trimmingCharacters(in: .decimalDigits)
        if value.count > 0 {
            return false
        }
        return true
    }
    
    /// 验证数字
    var cj_isNumber: Bool {
        let regex = "^[0-9]*$"
        let pred = NSPredicate(format: "SELF MATCHES %@", regex)
        let eval = pred.evaluate(with: self)
        return eval
    }
    
    /// 验证数字和小数点
    var cj_isNumberAndPoint: Bool {
        if self.count <= 0 {
            return false
        }
        let value = self.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789."))
        if value.count > 0 {
            return false
        }
        return true
    }
    
    /// 验证邮箱
    var cj_isEmail: Bool {
        let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let pred = NSPredicate(format: "SELF MATCHES %@", regex)
        let eval = pred.evaluate(with: self)
        return eval
    }
    
    /// 验证手机号码
    var cj_isPhone: Bool {
        let regex = "^1[0-9]{10}$"
        let pred = NSPredicate(format: "SELF MATCHES %@", regex)
        let eval = pred.evaluate(with: self)
        return eval
    }
    
    /// 验证邮政编码
    var cj_isPostal: Bool {
        let regex = "^[1-9]\\d{5}｜000000$"
        let pred = NSPredicate(format: "SELF MATCHES %@", regex)
        let eval = pred.evaluate(with: self)
        return eval
    }
    
    /// 验证全中文
    var cj_isAllChinese: Bool {
        let regex = "(^[\\u4E00-\\u9FA5]+$)"
        let pred = NSPredicate(format: "SELF MATCHES %@", regex)
        let eval = pred.evaluate(with: self)
        return eval
    }
    
    /// 简单验证身份证
    var cj_isIDCard: Bool {
        let regex = "^(\\d{14}|\\d{17})(\\d|[xX])$"
        let pred = NSPredicate(format: "SELF MATCHES %@", regex)
        let eval = pred.evaluate(with: self)
        return eval
    }
    
    /// 密码验证 既有数字又有字母
    var cj_isPassword: Bool {
        if self.count <= 0 {
            return false
        }
        
        var numCount = 0
        var letCount = 0
        // 检测数字
        let numRegularEx = try? NSRegularExpression(pattern: "[0-9]")
        if let numRegularEx = numRegularEx {
            numCount = numRegularEx.numberOfMatches(in: self, range: NSMakeRange(0, self.count))
        }
        // 检测字母
        let letRegularEx = try? NSRegularExpression(pattern: "[A-Za-z]")
        if let letRegularEx = letRegularEx {
            letCount = letRegularEx.numberOfMatches(in: self, range: NSMakeRange(0, self.count))
        }
        
        if numCount == self.count || letCount == self.count {
            return false
        } else if numCount + letCount == self.count {
            // 既有数字又有字母
            return true
        } else {
            return false
        }
    }
    
    /// 密码验证 既有数字又有字母 位数
    func cj_isPassword(min: Int, max: Int) -> Bool {
        let regex = String(format: "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{%ld,%ld}$", min, max)
        let pred = NSPredicate(format: "SELF MATCHES %@", regex)
        let eval = pred.evaluate(with: self)
        return eval
    }
    
    /// 验证url字符串
    var cj_isUrlString: Bool {
        let regex = "((http[s]{0,1}|ftp)://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)"
        let pred = NSPredicate(format: "SELF MATCHES %@", regex)
        let eval = pred.evaluate(with: self)
        return eval
    }
}
